package com.manchikanti.javacommons.util;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

public class HttpUtils {
  public static StringBuffer getContent(String webAddress,boolean ignoreWhiteSpaces, boolean ignoreNewLines) throws Exception {
	URL url = new URL(webAddress);
    URLConnection urlConnection = url.openConnection();
    HttpURLConnection connection = null;
    if (urlConnection instanceof HttpURLConnection) {
      connection = (HttpURLConnection) urlConnection;
      //setting http values for posting
      
    } else {
      throw new Exception("Invalid HTTP URL.");
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuffer urlContent = new StringBuffer();
    int current=0;
    System.out.println(in.read());
    while ((current = in.read()) >= 0) {
    	char c=(char)current;
    	System.out.println(c);
    	if(ignoreWhiteSpaces || ignoreNewLines){
    		if(ignoreWhiteSpaces){
    			if(c!=' ' && c != '\t'){
    				urlContent.append(c);
    			}
    		}
    		if(ignoreNewLines){
    			if(c!='\r' && c!='\n'){
    				urlContent.append(c);
    			}
    		}
    		
    	}
    	else{
    		urlContent.append(c);
    	}
    }
    return urlContent;
  }
  public static String getGzipContent(String webAddress,boolean ignoreWhiteSpaces, boolean ignoreNewLines) throws Exception {
	  
		URL url = new URL(webAddress);
		
	    URLConnection urlConnection = url.openConnection();
	    System.out.println("Got UrlConnection.");
	    HttpURLConnection connection = null;
	    if (urlConnection instanceof HttpURLConnection) {
	    	
	      connection = (HttpURLConnection) urlConnection;
	      //setting http values for posting
	      connection.setDoInput(true);
	      connection.setInstanceFollowRedirects(true);
//	      connection.setFollowRedirects(true);
	      connection.setDoOutput(true);	
	      connection.setRequestMethod("GET");
	      connection.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*");
	      connection.setRequestProperty("Accept-Language", "en-us");
	      connection.setRequestProperty("Content-Type", "text/html; charset=ISO-8859-1");
	      connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
//	      connection.setRequestProperty("Transfer-Encoding","chunked");
	      connection.setRequestProperty("Content-Encoding","gzip");
	      connection.setRequestProperty("Proxy-Connection", "Keep-Alive");
	      connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MEGAUPLOAD 2.0)");
	      connection.setRequestProperty("Proxy-Connection","Keep-Alive");
	      connection.setRequestProperty("Pragma","no-cache");
	      
	      
	    } else {
	      throw new Exception("Invalid HTTP URL.");
	    }
	    
	    
	    ByteArrayOutputStream out =  new ByteArrayOutputStream();
	    connection.setConnectTimeout(5000);
	    connection.setReadTimeout(5000);
	    System.out.println("Connectiong to conn.");
    	connection.connect();
    	System.out.println("Connected.");
    	int rC=connection.getResponseCode();
    	if (rC / 100 != 2) {
    		throw new Exception("Response code: "+rC);
    	}
    	GZIPInputStream in=null;
	    try {
	        // Open the compressed file
	         in= new GZIPInputStream(connection.getInputStream());
	        // Open the output file
//	        String outFilename = "amz.html";
//	        	new FileOutputStream(outFilename);
	        // Transfer bytes from the compressed file to the output file
	        byte[] buf = new byte[1024];
	        int len;
	        while ((len = in.read(buf)) > 0) {
	            out.write(buf, 0, len);
	        }
	    
	    } catch (IOException e) {
	    }finally{
	        // Close the file and stream
	        in.close();
	        out.close();
	    }
	    String urlContent=new String(out.toString());
	    
	    if(ignoreWhiteSpaces){
	    	urlContent=urlContent.replaceAll(""+' ', "");
	    	urlContent=urlContent.replaceAll(""+'\t', "");
	    }
	    if(ignoreNewLines){
	    	urlContent=urlContent.replaceAll(""+'\r', "");
	    	urlContent=urlContent.replaceAll(""+'\n', "");
	    }
	    try{
	    connection.disconnect();
	    }catch (Exception e) {
			e.printStackTrace();
		}
	    
	    return urlContent;
	    
	    
//	    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//	    StringBuffer urlContent = new StringBuffer();
//	    int current=0;
//	    System.out.println(in.read());
//	    while ((current = in.read()) >= 0) {
//	    	char c=(char)current;
//	    	System.out.println(c);
//	    	if(ignoreWhiteSpaces){
//		    	if(c!=' ' && c != '\t' && c!='\r' && c!='\n'){
//		    		urlContent.append(c);
//				}
//	    	}else{
//	    		urlContent.append(c);
//	    	}
//	    }
//	    return urlContent;
	  }
}